import React from 'react';
import { hasToken } from '../utils/storage';
import { Route, Redirect } from 'react-router-dom';

// function AuthRouter({ component: Component, ...rest }) {
export default function AuthRouter(props) {
  const { component: Comp, ...rest } = props;
  return (
    <Route
      {...rest}
      render={(props) => {
        if (hasToken()) {
          return <Comp {...props}></Comp>;
        } else {
          return (
            <Redirect
              to={{ pathname: '/login', state: { from: props.location } }}
            ></Redirect>
          );
        }
      }}
    ></Route>
  );
}
